//
//  InputPasswordView.m
//  CedarFinancePH
//
//  Created by lshenrong on 2018/4/8.
//  Copyright © 2018年 cedarhd. All rights reserved.
//

#import "InputPasswordView.h"
#include <UIView+TYAlertView.h>

@interface InputPasswordView()

@property (nonatomic,strong) NSMutableArray *inputTextFieldArray;


@end


@implementation InputPasswordView

//316 X225
- (instancetype)init{
    if (self = [super init]) {
        self.backgroundColor=[UIColor whiteColor];
        self.layer.cornerRadius = 12.0;
        self.layer.masksToBounds = YES;
        self.frame = CGRectMake(0, 0, 316, 225);
        [self setupSubviews];
    }
    return self;
}


- (void)textFieldDidChange:(UITextField *)textField{
    if (textField.text.length > 1) {
        textField.text = [textField.text substringToIndex:1];
    }
}



- (void)setupSubviews{
    UILabel *titleLbl = [[UILabel alloc]init];
    MACRO_UILabel_Configure(titleLbl, @"请输入交易密码", kBlackColor, 17);
    titleLbl.numberOfLines = 0;
    [self addSubview:titleLbl];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).offset(-28);
        make.top.equalTo(self).offset(35);
    }];
    UITextField *firstTextField =  nil;
    for (NSInteger i = 0; i<6; i++) {
        UITextField *textField =  [[UITextField alloc]init];
        [self.inputTextFieldArray addObject:textField];
        if (i == 0) {
            firstTextField = textField;
        }
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        textField.textAlignment = NSTextAlignmentCenter; //水平左对齐
        textField.layer.borderColor = kMasterColor.CGColor;
        textField.layer.borderWidth= 0.5f;
        textField.tag = 1000 + i;
        textField.keyboardType = UIKeyboardTypeNumberPad;
        [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [self addSubview:textField];
        
        CGFloat startX = 24.0 + 45*i;
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(startX);
            make.top.equalTo(titleLbl.mas_bottom).offset(35);
            make.size.mas_offset(CGSizeMake(45, 45));
        }];
    }
    
    CedarButton *forgetPwdBtn =  [CedarButton buttonWithTextNoStytle:@"忘记密码?"];
    [forgetPwdBtn PFCGStyleFontSize:15];
    [self addSubview:forgetPwdBtn];
    [forgetPwdBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-15);
        make.top.mas_equalTo(firstTextField.mas_bottom).mas_offset(5);
        make.size.mas_offset(CGSizeMake(82, 30));
    }];
    
    UIView *hLineView =  [[UIView alloc]init];
    hLineView.backgroundColor = kSeparateLineColor;
    [self addSubview:hLineView];
    [hLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self);
        make.top.mas_equalTo(forgetPwdBtn.mas_bottom).mas_offset(6);
        make.height.mas_offset(0.5);
    }];
    
    CedarButton *cancelBtn =  [CedarButton buttonWithTextNoStytle:@"取消"];
    @weakify(self)
    cancelBtn.clickEvent = ^{
        @strongify(self)
        [self hideView];
    };
    [cancelBtn PFCGStyleTextColor:kGrayColor fontSize:18.0];
    [self addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).mas_offset(61);
        make.top.mas_equalTo(hLineView).mas_offset(5);
        make.bottom.mas_equalTo(self).mas_offset(-5);
    }];
    

    
    UIView *vLineView =  [[UIView alloc]init];
    vLineView.backgroundColor = kSeparateLineColor;
    [self addSubview:vLineView];
    [vLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(hLineView).mas_offset(10);
        make.bottom.mas_equalTo(self).mas_offset(-10);
        make.width.mas_offset(0.5);
    }];
    
    CedarButton *rechargeBtn =  [CedarButton buttonWithTextNoStytle:@"确定"];
    rechargeBtn.clickEvent = ^{
        NSMutableString *mStr =  [NSMutableString string];
        for (UITextField *textField in self.inputTextFieldArray) {
            [mStr appendString:textField.text];
        }
        NSString *pwd = [mStr copy];
        CLog(@"pwd:%@",pwd);
        if (self.confirmHandelr) {
            self.confirmHandelr(pwd);
        }
    };
    [rechargeBtn PFCGStyleTextColor:kMasterColor fontSize:18.0];
    [self addSubview:rechargeBtn];
    [rechargeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self).mas_offset(-61);
        make.centerY.mas_equalTo(cancelBtn);
    }];
}

- (NSMutableArray *)inputTextFieldArray{
    if (!_inputTextFieldArray) {
        _inputTextFieldArray = [NSMutableArray arrayWithCapacity:6];
    }
    return _inputTextFieldArray;
}


@end
